using System.Collections.Generic;
using System.Windows;
using System.Windows.Markup;
using Common;
using SProject;

namespace ProjectView.Windows
{
	public partial class ProjectEncryptWindow : Window, IComponentConnector
	{
		private Project _Project;

		private List<Node> nodesList;

		public Project Project
		{
			set
			{
				_Project = value;
				RefreshNodeList();
			}
		}

		public ProjectEncryptWindow()
		{
			InitializeComponent();
			nodesList = new List<Node>
			{
				new Node
				{
					ID = 1000,
					Name = Utility.GetMessage("ProjectFile"),
					ParentId = -1
				},
				new Node
				{
					ID = 1100,
					Name = Utility.GetMessage("Prj_BasicInfo"),
					ParentId = 1000
				},
				new Node
				{
					ID = 1110,
					Name = Utility.GetMessage("Prj_PrjBasicInfo"),
					ParentId = 1100
				},
				new Node
				{
					ID = 1120,
					Name = Utility.GetMessage("Prj_TargetInfo"),
					ParentId = 1100
				},
				new Node
				{
					ID = 1200,
					Name = Utility.GetMessage("Prj_Program_title"),
					ParentId = 1000
				},
				new Node
				{
					ID = 1300,
					Name = Utility.GetMessage("Prj_Parameter"),
					ParentId = 1000
				},
				new Node
				{
					ID = 1310,
					Name = Utility.GetMessage("Prj_AnalysisParameter"),
					ParentId = 1300
				},
				new Node
				{
					ID = 1320,
					Name = Utility.GetMessage("Prj_QuantitativeRange"),
					ParentId = 1300
				},
				new Node
				{
					ID = 1330,
					Name = Utility.GetMessage("Prj_Crosstalk"),
					ParentId = 1300
				},
				new Node
				{
					ID = 1400,
					Name = Utility.GetMessage("Prj_RuleConclusion"),
					ParentId = 1000
				}
			};
			List<Node> list = new List<Node> { nodesList[0] };
			Node node = list[0];
			node.AddChild(nodesList[1]);
			node.AddChild(nodesList[4]);
			node.AddChild(nodesList[5]);
			node.AddChild(nodesList[9]);
			Node node2 = list[0].Children[0];
			node2.AddChild(nodesList[2]);
			node2.AddChild(nodesList[3]);
			Node node3 = list[0].Children[2];
			node3.AddChild(nodesList[6]);
			node3.AddChild(nodesList[7]);
			node3.AddChild(nodesList[8]);
			treeView.ItemsSource = list;
		}

		private void RefreshNodeList()
		{
			if (_Project != null)
			{
				nodesList[2].IsReadOnly = _Project.BasicInfo.IsReadOnly;
				nodesList[3].IsReadOnly = _Project.Target.IsReadOnly;
				nodesList[4].IsReadOnly = _Project.PCRProgram.IsReadOnly;
				nodesList[6].IsReadOnly = _Project.BasicOption.IsReadOnly;
				nodesList[7].IsReadOnly = _Project.BasicOption.IsDetectionReadOnly;
				nodesList[8].IsReadOnly = _Project.CrossTalk.IsReadOnly;
				nodesList[9].IsReadOnly = _Project.Rule.IsReadOnly;
				if (!string.IsNullOrEmpty(_Project.Password))
				{
					passwordBoxEdit.Password = _Project.Password;
					passwordBoxEditRepeat.Password = _Project.Password;
				}
			}
		}

		private void btnOK_Click(object sender, RoutedEventArgs e)
		{
			if (_Project == null)
			{
				return;
			}
			if (passwordBoxEdit.Password.Length > 0 && passwordBoxEdit.Password.Length < 6)
			{
				MessageBox.Show(string.Format(Utility.GetMessage("msg_password_length_less"), 6));
				passwordBoxEdit.SelectAll();
				passwordBoxEdit.Focus();
				return;
			}
			if (passwordBoxEditRepeat.Password.Length > 0 && passwordBoxEditRepeat.Password.Length < 6)
			{
				MessageBox.Show(string.Format(Utility.GetMessage("msg_password_length_less"), 6));
				passwordBoxEditRepeat.SelectAll();
				passwordBoxEditRepeat.Focus();
				return;
			}
			if (passwordBoxEdit.Password.Length > 0 && passwordBoxEditRepeat.Password.Length == 0)
			{
				MessageBox.Show(Utility.GetMessage("msg_password_not_input"));
				passwordBoxEditRepeat.SelectAll();
				passwordBoxEditRepeat.Focus();
				return;
			}
			if (passwordBoxEdit.Password.Length == 0 && passwordBoxEditRepeat.Password.Length > 0)
			{
				MessageBox.Show(Utility.GetMessage("msg_password_not_input"));
				passwordBoxEdit.SelectAll();
				passwordBoxEdit.Focus();
				return;
			}
			if (passwordBoxEdit.Password.Length > 0 && passwordBoxEditRepeat.Password.Length > 0)
			{
				if (!passwordBoxEdit.Password.Equals(passwordBoxEditRepeat.Password))
				{
					MessageBox.Show(Utility.GetMessage("msg_password_not_same"));
					passwordBoxEdit.SelectAll();
					passwordBoxEdit.Focus();
					return;
				}
				_Project.Password = passwordBoxEdit.Password;
			}
			_Project.BasicInfo.IsReadOnly = nodesList[2].IsReadOnly == true;
			_Project.Target.IsReadOnly = nodesList[3].IsReadOnly == true;
			_Project.PCRProgram.IsReadOnly = nodesList[4].IsReadOnly == true;
			_Project.BasicOption.IsReadOnly = nodesList[6].IsReadOnly == true;
			_Project.BasicOption.IsDetectionReadOnly = nodesList[7].IsReadOnly == true;
			_Project.CrossTalk.IsReadOnly = nodesList[8].IsReadOnly == true;
			_Project.Rule.IsReadOnly = nodesList[9].IsReadOnly == true;
			base.DialogResult = true;
			Close();
		}

		private void btnCancel_Click(object sender, RoutedEventArgs e)
		{
			Close();
		}
	}
}
